﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Forms;
using WallpaperSharing.AppLibrary.Extensions;
using WallpaperSharing.DataAccess;
using WallpaperSharing.DataAccess.Interfaces;
using WallpaperSharing.Domain;
using System.Drawing;

namespace WallpaperSharing.AppLibrary.Forms
{
    public partial class AddWallpaperForm : Form
    {
        private readonly ICategoryDao _categoryDao;
        private readonly IWallpaperDao _wallpaperDao;
        private readonly IWallpaperCategoryDao _wallpaperCategoryDao;
        private readonly OpenFileDialog _openFileDialog;

        public delegate void WallpaperAddedCallback(IList<int> categoriesIds);
        public event WallpaperAddedCallback WallpaperAdded;


        public AddWallpaperForm()
        {
            InitializeComponent();
            _openFileDialog = new OpenFileDialog();
            _openFileDialog.Filter = "Image Files (*.bmp, *.jpg, *.png, *.tiff, *.gif)|*.bmp;*.jpg;*.jpeg;*.gif;*.tiff;*.png";
            _categoryDao = new CategoryDao();
            _wallpaperDao = new WallpaperDao();
            _wallpaperCategoryDao = new WallpaperCategoryDao();

            IList<Category> categories = _categoryDao.SelectAll();
            lstCategory.DataSource = categories;
            lstCategory.DisplayMember = "Name";
        }

        private void ClearValues()
        {
            txtBrowse.Clear();
            txtName.Clear();
            txtWidth.Clear();
            txtHeight.Clear();
            txtExtension.Clear();
            lstCategory.SelectedItems.Clear();
            imgWallpaper.Image = null;
        }

        private void browseBtn_Click(object sender, EventArgs e)
        {
            if (_openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtBrowse.Text = _openFileDialog.FileName;
                imgWallpaper.Load(_openFileDialog.FileName);

                string fileName = _openFileDialog.SafeFileName;
                txtName.Text = fileName.Substring(0,fileName.LastIndexOf("."));
                txtWidth.Text = imgWallpaper.Image.Width.ToString(CultureInfo.InvariantCulture);
                txtHeight.Text = imgWallpaper.Image.Height.ToString(CultureInfo.InvariantCulture);
                txtExtension.Text = fileName.Substring(fileName.LastIndexOf("."));
                addBtn.Enabled = true;

            }

        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void addBtn_Click(object sender, EventArgs e)
        {
            Wallpaper wallpaper = AddWallpaper();
            IList<int> categoriesIds = AddCategoriesFor(wallpaper);

            Close();
            ClearValues();

            if (WallpaperAdded != null)
            {
                WallpaperAdded(categoriesIds);
            }
        }

        private Wallpaper AddWallpaper()
        {
            Image image = Image.FromFile(txtBrowse.Text);
            var wallpaper = new Wallpaper
            {
                Name = txtName.Text,
                Width = Int32.Parse(txtWidth.Text),
                Height = Int32.Parse(txtHeight.Text),
                Extension = txtExtension.Text,
                File = image.ToBytes()
            };
            _wallpaperDao.Insert(wallpaper);

            return wallpaper;
        }

        private IList<int> AddCategoriesFor(Wallpaper wallpaper)
        {
            var categoriesIds = new List<int>();

            if (lstCategory.SelectedItems.Count > 0)
            {
                foreach (Category category in lstCategory.SelectedItems)
                {
                    var wallpaperCategory = new WallpaperCategory
                    {
                        WallpaperId = wallpaper.Id,
                        CategoryId = category.Id
                    };
                    _wallpaperCategoryDao.Insert(wallpaperCategory);
                    categoriesIds.Add(category.Id);
                }
            }

            return categoriesIds;
        }
    }
}
